using System;
using System.Data;
using System.Configuration;
using System.Web;

using System.Net.Mail;
using Jamila2.CoreElements;

public class EmailFactory : ManagerBase, IDisposable
{
    SmtpClient MailClient = null;
    MailMessage CurrentContent = null;
    MailAddress SendFrom = null, SendTo = null;
    bool mail_ready = false;

    public bool IsReady
    {
        get
        {
            return this.mail_ready;
        }
    }

    public EmailFactory(string ToName, string ToEmail, string Subject, string Content, string SMTP)
    {
        try
        {
            this.SendTo = new MailAddress(ToEmail, ToName);
            this.SendFrom = new MailAddress("admin@covidien.com", "eClaim Admin");

            CurrentContent = new MailMessage(this.SendFrom, this.SendTo);
            CurrentContent.Subject = Subject;
            CurrentContent.SubjectEncoding = System.Text.Encoding.UTF8;
            CurrentContent.Body = Content;
            CurrentContent.BodyEncoding = System.Text.Encoding.UTF8;
            CurrentContent.IsBodyHtml = true;

            MailClient = new SmtpClient(SMTP);

            this.mail_ready = true;
        }
        catch (Exception ex)
        {
            this.ErrMsg = "[EmailFactory] : " + ex.Message;
        }
    }

    public bool SendNow()
    {
        bool smooth = true;

        try
        {
            this.MailClient.Send(this.CurrentContent);
        }
        catch (Exception ex)
        {
            // xxx
            smooth = false;
            this.ErrMsg = "[EmailFactory.SendNow] : " + ex.Message;
        }

        return smooth;
    }

    public override void Dispose()
    {
        this.MailClient = null;

        base.Dispose();
    }
}
